package com.sunny.ihealth.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.sunny.ihealth.R;
import com.sunny.ihealth.entity.bmob.UserInfo;
import com.sunny.ihealth.entity.bmob.UserRelation;

import java.util.List;

/**
 * 好友和粉丝的adapter
 * Created by Sunny  An on 2016/6/5.
 */
public class FriendAndFansAdapter extends RecyclerView.Adapter<FriendAndFansAdapter.Holder> implements View.OnClickListener{

    public static final int TYPE_FRIEND = 0;
    public static final int TYPE_FANS = 1;

    private Context context;
    private List<UserRelation> data;
    private LayoutInflater inflater;
    private int type;
    private OnViewClickListener listener;

    public FriendAndFansAdapter(Context context, List<UserRelation> data, int type) {
        this.context = context;
        this.data = data;
        this.type = type;
        inflater = LayoutInflater.from(this.context);
    }

    @Override
    public FriendAndFansAdapter.Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new Holder(inflater.inflate(R.layout.item_friend, parent, false));
    }

    @Override
    public void onBindViewHolder(FriendAndFansAdapter.Holder holder, int position) {
        UserRelation relation = data.get(position);
        UserInfo user;
        if (type == TYPE_FRIEND) {
            user = relation.getFriend();
        } else {
            user = relation.getUser();
        }

        if (user.hasHead()) {
            Picasso.with(context).load(user.getHeadFile().getUrl()).into(holder.ivHead);
        }
        holder.tvNickname.setText(user.getNickname());
        holder.ivGender.setImageResource(user.getGender() == UserInfo.GENDER_MALE ? R.drawable.ic_boy : R.drawable.ic_girl);
        if (TextUtils.isEmpty(user.getSignature())) {
            holder.tvSignature.setVisibility(View.GONE);
        } else {
            holder.tvSignature.setVisibility(View.VISIBLE);
            holder.tvSignature.setText(user.getSignature());
        }


        holder.btnMore.setOnClickListener(this);
        holder.llItem.setOnClickListener(this);
        holder.setTag(position);
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    @Override
    public void onClick(View v) {
        if (listener == null) {
            return;
        }
        int position = (int) v.getTag();
        listener.onViewClick(v.getId(), position);
    }

    public OnViewClickListener getListener() {
        return listener;
    }

    public void setListener(OnViewClickListener listener) {
        this.listener = listener;
    }

    class Holder extends RecyclerView.ViewHolder {

        LinearLayout llItem;
        ImageView ivHead, ivGender;
        TextView tvNickname, tvSignature;
        ImageButton btnMore;

        public Holder(View v) {
            super(v);
            llItem = (LinearLayout) v.findViewById(R.id.ll_itemFriend);
            ivHead = (ImageView) v.findViewById(R.id.image_itemFriend_head);
            ivGender = (ImageView) v.findViewById(R.id.image_itemFriend_gender);
            tvNickname = (TextView) v.findViewById(R.id.tv_itemFriend_nickname);
            tvSignature = (TextView) v.findViewById(R.id.tv_itemFriend_signature);
            btnMore = (ImageButton) v.findViewById(R.id.btn_itemFriend_more);
        }

        public void setTag(Object tag) {
            llItem.setTag(tag);
            btnMore.setTag(tag);
        }
    }
}
